package com.xuexiang.xuidemo.slice.picker;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;

import com.xuexiang.xui_lib.component.button.SuperButton;
import com.xuexiang.xui_lib.component.picker.RulerView;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.TextUtils;
import com.xuexiang.xuidemo.util.ToastUtil;

/**
 *
 * 支持选择身高、体重、视力的尺子
 */
public class RulerViewSlice extends AbilitySlice implements Component.ClickedListener {
    private Text text_title,text_title_bottom;
    private SuperButton btn_set,btn_get;
    private TextField etWeight;
    private RulerView rulerView;
    private Image image_back;
    private DirectionalLayout dl;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_rulerview);
        initComponent();
        initTitle();
        initClickedListener();
    }
    public void  initComponent(){
        image_back=(Image) findComponentById(ResourceTable.Id_image_back);
        text_title=(Text)findComponentById(ResourceTable.Id_text_title);
        text_title_bottom=(Text)findComponentById(ResourceTable.Id_text_title_bottom);
        etWeight=(TextField)findComponentById(ResourceTable.Id_et_weight);
        rulerView=(RulerView) findComponentById(ResourceTable.Id_rulerView);
        btn_set=(SuperButton)findComponentById(ResourceTable.Id_btn_set);
        btn_get=(SuperButton)findComponentById(ResourceTable.Id_btn_get);
        dl=(DirectionalLayout)findComponentById(ResourceTable.Id_dl);
    }
    private void initTitle() {
        text_title.setText("RulerView");
        text_title_bottom.setText("支持选择身高、体重、视力的尺子");
        btn_get.setClickedListener(this);
        btn_set.setClickedListener(this);
        dl.setClickedListener(this);
    }
    private void initClickedListener() {
        image_back.setClickedListener(this);
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_image_back:
                onBackPressed();
                break;
            case ResourceTable.Id_btn_set:
                etWeight.clearFocus();
                if (!TextUtils.isEmpty(etWeight.getText().toString())) {
                    rulerView.setCurrentValue(TextUtils.toFloat(etWeight.getText().toString(), 0));
                } else {
                    ToastUtil.showToast(this,"请输入体重值！");
                }
                break;
            case ResourceTable.Id_btn_get:
                ToastUtil.showToast(this,"体重:"+rulerView.getCurrentValue());
                etWeight.clearFocus();
                break;
            default:
                etWeight.clearFocus();
                break;
        }
    }
}
